home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
rsynth
/
src
/
trie.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
1KB
|
77 lines
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include "proto.h"
#include "trie.h"
struct trie_s
{
struct trie_s *otherwise;
struct trie_s *more;
void *value;
char ch;
};
void trie_insert(r,s,value)
trie_ptr *r;
char *s;
void *value;
{
trie_ptr p = NULL;
char ch;
while ((ch = *s++))
{
while ((p = *r))
{
if (p->ch == ch)
break;
else
r = &p->otherwise;
}
if (!p)
{
p = (trie_ptr) malloc(sizeof(*p));
memset(p,0,sizeof(*p));
p->ch = ch;
*r = p;
}
r = &p->more;
}
p->value = value;
}
void *trie_lookup(r,sp)
trie_ptr *r;
char **sp;
{char *s = *sp;
char *value = NULL;
char ch;
while ((ch = *s))
{
trie_ptr *l = r;
trie_ptr p;
while ((p = *l))
{
if (p->ch == ch)
break;
else
l = &p->otherwise;
}
if (p)
{
*l = p->otherwise;
p->otherwise = *r;
*r = p;
r = &p->more;
value = p->value;
s++;
}
else
break;
}
*sp = s;
return value;
}